package controller;

import io.javalin.http.Context;
import service.ResourceService;
import util.JwtUtil;
import util.ResponseUtil;
import util.ValidationUtil;

import java.util.Map;

public class ResourceController {
    private static final ResourceService resourceService = new ResourceService();
    
    /**
     * 上传音乐接口
     */
    public static void uploadMusic(Context ctx) {
        try {
            String token = ctx.header("Authorization");
            if (token == null || !token.startsWith("Bearer ")) {
                ctx.json(ResponseUtil.unauthorized());
                return;
            }
            
            Long userId = JwtUtil.getUserIdFromToken(token.substring(7));
            String title = ctx.formParam("title");
            String artist = ctx.formParam("artist");
            
            // 添加日志
            System.out.println("开始上传音乐:");
            System.out.println("userId: " + userId);
            System.out.println("title: " + title);
            System.out.println("artist: " + artist);
            System.out.println("musicFile: " + (ctx.uploadedFile("musicFile") != null));
            System.out.println("coverFile: " + (ctx.uploadedFile("coverFile") != null));
            System.out.println("lyricFile: " + (ctx.uploadedFile("lyricFile") != null));
            
            // 验证参数
            ValidationUtil.validateMusicTitle(title);
            ValidationUtil.validateArtist(artist);
            
            Map<String, Object> result = resourceService.uploadMusic(
                userId,
                title,
                artist,
                ctx.uploadedFile("musicFile"),
                ctx.uploadedFile("coverFile"),
                ctx.uploadedFile("lyricFile")
            );
            
            // 检查结果
            System.out.println("上传结果: " + result);
            
            ctx.status(201).json(result);
            
        } catch (Exception e) {
            e.printStackTrace();
            ctx.json(ResponseUtil.error("上传失败：" + e.getMessage()));
        }
    }
    
    /**
     * 删除音乐接口
     */
    public static void deleteMusic(Context ctx) {
        try {
            String token = ctx.header("Authorization");
            if (token == null || !token.startsWith("Bearer ")) {
                ctx.json(ResponseUtil.unauthorized());
                return;
            }
            
            Long userId = JwtUtil.getUserIdFromToken(token.substring(7));
            Long songId = Long.parseLong(ctx.pathParam("id"));
            
            resourceService.deleteMusic(userId, songId);
            ctx.json(ResponseUtil.success("音乐删除成功"));
            
        } catch (IllegalArgumentException e) {
            ctx.json(ResponseUtil.error(e.getMessage()));
        } catch (Exception e) {
            e.printStackTrace();
            ctx.json(ResponseUtil.error("删除失败：" + e.getMessage()));
        }
    }
    
    /**
     * 获取用户上传的音乐列表
     */
    public static void getUserMusics(Context ctx) {
        try {
            String token = ctx.header("Authorization");
            if (token == null || !token.startsWith("Bearer ")) {
                ctx.json(ResponseUtil.unauthorized());
                return;
            }
            
            Long userId = JwtUtil.getUserIdFromToken(token.substring(7));
            ctx.json(resourceService.getUserMusics(userId));
            
        } catch (Exception e) {
            e.printStackTrace();
            ctx.json(ResponseUtil.error("获取失败：" + e.getMessage()));
        }
    }
    
    /**
     * 获取音乐源文件接口
     */
    public void getMusicSource(Context ctx) {
        // 获取音乐源文件的逻辑,暂时返回一个固定的响应
        ctx.json(ResponseUtil.success("Music source"));
    }
}